<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        .color-preview{
            display: inline-block;
            width: 50px;
            height: 50px;
        }
    </style>
  </head>
  <body>
    <div id="app">
      <input
        v-model="colorQuery"
        @keyup.enter="updateColor"
        placeholder="Enter a Color"
      />
      <button type="button" @click="updateColor">Update</button>
      <p>Preview:</p>
      <span
        :style="{backgroundColor:tweenedCSSColor}"
        class="color-preview"
      ></span>
      <p>{{tweenedCSSColor}}</p>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/tween.js/16.3.4/Tween.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/color-js/1.0.1/color.min.js"></script>
    <script>
      var Color = net.brehaut.Color;

      new Vue({
        el: "#app",
        data: {
          colorQuery: "",
          color: {
            red: 0,
            green: 0,
            blue: 0,
            alpha: 1,
          },
          tweenedColor: {},
        },
        created: function () {
          this.tweenedColor = Object.assign({}, this.color);
        },
        watch: {
          color: function () {
            function animate() {
              if (TWEEN.update()) {
                requestAnimationFrame(animate);
              }
            }

            new TWEEN.Tween(this.tweenedColor).to(this.color, 750).start();

            animate();
          },
        },
        computed: {
          tweenedCSSColor: function () {
            return new Color({
              red: this.tweenedColor.red,
              green: this.tweenedColor.green,
              blue: this.tweenedColor.blue,
              alpha: this.tweenedColor.alpha,
            }).toCSS();
          },
        },
        methods: {
          updateColor: function () {
            this.color = new Color(this.colorQuery).toRGB();
            this.colorQuery = "";
          },
        },
      });
    </script>
  </body>
</html>
